/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/9/27 2:52 下午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import {
    reqBreedList,
    reqDetailProduct,
    reqGetPcategory,
    reqUpdateProduct,
} from 'api/index';

export default {
    namespace: 'adoptProductEditDetail',
    state: {
        productDetail: {},
        pcategoryOption: [],
        breedList: [],
        intro: '',
        thumbnail: [],
        big_images: [],
    },
    reducers: {
        setProductDetail(state, { payload: productDetail }) {
            return { ...state, productDetail };
        },
        setPcategoryOption(state, { payload: pcategoryOption }) {
            return { ...state, pcategoryOption };
        },
        setBreedList(state, { payload: breedList }) {
            return { ...state, breedList };
        },
        setIntro(state, { payload: intro }) {
            return { ...state, intro };
        },
        setThumbnail(state, { payload: thumbnail }) {
            return { ...state, thumbnail };
        },
        set_big_images(state, { payload: big_images }) {
            return { ...state, big_images };
        },

    },
    effects: {
        * getProductDetail({ payload }, { call, put, select }) {
            const result = yield call(reqDetailProduct, payload);
            if (result.status === 1) {
                const productDetail = result.data;
                yield put({
                    type: 'setProductDetail',
                    payload: productDetail,
                });

                const intro = result.data.intro;
                yield put({
                    type: 'setIntro',
                    payload: intro,
                });

                const thumbnail = result.data.spec_items[0].thumbnail.split(',').map((item, index) => {
                    return {
                        uid: index,
                        name: item,
                        status: 'done',
                        url: item,
                    };
                });
                yield put({
                    type: 'setThumbnail',
                    payload: thumbnail,
                });

                const big_images = result.data.big_images.map((item, index) => {
                    return {
                        uid: index,
                        name: item,
                        status: 'done',
                        url: item,
                    };
                });
                yield put({
                    type: 'set_big_images',
                    payload: big_images,
                });

            } else {
                message.error(result.message);
            }
        },

        * getPcategoryOption({ _ }, { call, put, select }) {
            const result = yield call(reqGetPcategory);
            if (result.status === 1) {
                const pcategoryOption = result.data;
                yield put({
                    type: 'setPcategoryOption',
                    payload: pcategoryOption,
                });
            } else {
                message.error(result.message);
            }
        },
        * getBreedList({ _ }, { call, put, select }) {
            const result = yield call(reqBreedList);
            if (result.status === 1) {
                const breedList = result.data;
                yield put({
                    type: 'setBreedList',
                    payload: breedList,
                });
            } else {
                message.error(result.message);
            }
        },
        * updateProduct({ payload, callback }, { call, put, select }) {
            const result = yield call(reqUpdateProduct, payload);
            if (result.status === 1) {
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if (pathToRegexp('/product-manage/adopt-product/edit').exec(pathname)) {
                    dispatch({
                        type: 'getProductDetail',
                        payload: {
                            p_id: query.id,
                        },
                    });
                    dispatch({
                        type: 'getPcategoryOption',
                    });
                    dispatch({
                        type: 'getBreedList',
                    });
                }
            });
        },
    },
};
